// src/services/semantic_conversion.rs - 核心语义转换服务

use async_trait::async_trait;
use std::sync::Arc;
use anyhow::{Result, anyhow};
use chrono::{Utc};
use serde::{Deserialize, Serialize};

use crate::{
    interfaces::{DataSourceAdapter, OPCUAModelBuilder},
    models::{DataSourceSchema, OPCUAModel, ConversionRequest, ConversionResult},
};

/// 语义转换服务 - 核心业务逻辑
pub struct SemanticConversionService<A, B>
where
    A: DataSourceAdapter + Send + Sync,
    B: OPCUAModelBuilder + Send + Sync,
{
    data_adapter: Arc<A>,
    model_builder: Arc<B>,
}

impl<A, B> SemanticConversionService<A, B>
where
    A: DataSourceAdapter + Send + Sync,
    B: OPCUAModelBuilder + Send + Sync,
{
    pub fn new(data_adapter: A, model_builder: B) -> Self {
        Self {
            data_adapter: Arc::new(data_adapter),
            model_builder: Arc::new(model_builder),
        }
    }

    /// 执行语义转换
    pub async fn convert(&self, request: ConversionRequest) -> Result<ConversionResult> {
        // 1. 从数据源提取schema
        let schema = self.data_adapter.extract_schema(&request.source_config).await?;
        
        // 2. 构建OPC UA模型
        let model = self.model_builder.build_model(&schema, &request.mapping_rules).await?;
        
        // 3. 返回转换结果
        Ok(ConversionResult {
            request_id: request.id,
            model,
            statistics: Default::default(),
            status: crate::models::ConversionStatus::Completed,
            timestamp: Utc::now(),
        })
    }
}